<%--
  Created by IntelliJ IDEA.
  User: hejx
  Date: 2017/5/19
  Time: 15:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--头部-->
    <jsp:include page="/template/header.jsp" />
    <!--头部结束-->
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>添加角色</title>
    <link rel="shortcut icon" href="<%=basePath%>static/favicon.ico">
    <link href="<%=basePath%>static/css/plugins/iCheck/custom.css" rel="stylesheet">
    <!-- 引入zTree_v3 -->
    <link rel="stylesheet" href="<%=basePath%>static/plugins/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">

</head>
<body class="gray-bg">
    <div ng-app="myApp" ng-controller="addRoleController" class="wrapper wrapper-content scroll-wrapper">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form method="get" class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">职位ID<span style="color: red;">*</span></label>

                                <div class="col-sm-10">
                                    <input ng-blur="checkPkId()"  ng-model="role.pkId" type="number" min="1" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">角色名称<span style="color: red;">*</span></label>

                                <div class="col-sm-10">
                                    <input  ng-model="role.roleName" type="text" class="form-control">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">角色描述</label>

                                <div class="col-sm-10">
                                    <input  ng-model="role.roleDesc" type="text" class="form-control">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div id="ztreediv" class="form-group">
                                <label class="col-sm-2 control-label">选择角色资源</label>
                                <div class="col-sm-10">
                                    <ul id="treeDemo" class="ztree"></ul>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-primary" ng-click="save()" id="subBtn" type="button">保存内容</button>
                                    <button class="btn btn-white" onclick="parent.closeWin()" type="button">取消</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 导入尾部公共js -->
    <jsp:include page="/template/tail.jsp" />
    <script type="text/javascript" src="<%=basePath%>static/plugins/zTree_v3/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/plugins/zTree_v3/js/jquery.ztree.excheck.js"></script>


<script>

    $(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});

    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    var code;

    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            py = $("#py").attr("checked")? "p":"",
            sy = $("#sy").attr("checked")? "s":"",
            pn = $("#pn").attr("checked")? "p":"",
            sn = $("#sn").attr("checked")? "s":"",
            type = { "Y" : "ps", "N" : "ps" };
        zTree.setting.check.chkboxType = type;
        showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
    }
    function showCode(str) {
        if (!code) code = $("#code");
        code.empty();
        code.append("<li>"+str+"</li>");
    }

    $(document).ready(function(){

        var zNodes = JSON.parse('${treeNodes}');

        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        setCheck();
        $("#py").bind("change", setCheck);
        $("#sy").bind("change", setCheck);
        $("#pn").bind("change", setCheck);
        $("#sn").bind("change", setCheck);

        var h = document.getElementById("ztreediv").offsetHeight;   //获取树节点的高度
        $("#mainDiv").css("height",160+h+"px");

    });


    app.controller('addRoleController', function($scope,$http) {

        $scope.role;
        $scope.url;
        var oldPkId = "-1";

        if('${update}'=='6'){
            $scope.role = angular.fromJson('${role}');
            $scope.url = "/admin/role/update";
            oldPkId = $scope.role.pkId;
        }else{
            $scope.role = {
                pkId:'',
                roleName:'',
                roleDesc:'',
                reslist:''
            };
            $scope.url = "/admin/role/add";
        }

        //校验参数 return boolean
        $scope.validateParam = function(){
            if(isNull($scope.role.pkId)){
                layer.msg("请输入职位ID");
                return true;
            }
            if(isNull($scope.role.roleName)){
                layer.msg("请输入角色名称");
                return true;
            }
            if(isNull($scope.role.reslist)){
                layer.msg("请勾选角色权限");
                return true;
            }
            return false;
        }

        //save操作
        $scope.save = function(){

            //$("#treeDemo")
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getCheckedNodes(true);  //checked = true 表示获取 被勾选 的节点集合
            $scope.role.reslist = [];
            for(var i=0;i<nodes.length;i++) {
                $scope.role.reslist[i] = nodes[i].id;
            }

            if($scope.validateParam()){
                return false;
            }

            $('#subBtn')[0].disabled = true;
//            var jsonStr = angular.toJson($scope.role,true);
//            console.log(jsonStr);
            $scope.role.oldPkId = oldPkId;
            console.log($scope.role);

            //提交数据
            $http({method : 'POST', data:$scope.role, url : $scope.url})
                .success(function(data,status,headers,config){
                    //console.log(data);
                    $('#subBtn')[0].disabled = false;
                    layer.msg(data.msg);
                    if(data.code==0){
                        parent.closeWinAndFlush();
                    }
                }).error(function(data,status,headers,config){
                $('#subBtn')[0].disabled = false;
                console.log('error..........');
                console.log(data);
            });
        }

        //判断输入的ID是否可用
        $scope.checkPkId = function () {
            if (isNull($scope.role.pkId)) {
                return false;
            }
            var url = "/admin/role/searchRole";
            var param = {
                'newPkId': $scope.role.pkId,
                'oldPkId': oldPkId
            };
            $http({method: 'POST', data: param, url: url})
                .success(function (data, status, headers, config) {
                    if (data.code != 0) {
                        layer.alert(data.msg, {
                            skin: 'layui-layer-molv' //样式类名
                            , closeBtn: 0,
                            end: function () {
                                location.reload();
                            }
                        });
                    }
                }).error(function (data, status, headers, config) {
                console.log('error..........');
                console.log(data);
            });
        }
    });

</script>
</body>


<!-- Mirrored from www.zi-han.net/theme/hplus/form_basic.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:15 GMT -->
</html>